
$(document).ready(function(){
    // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
    $("#setting-confirm-btn").click(function(e){
        var modal = $("#modal1");
        var serialPort = (modal.find("select").val());
        KeyValue.set("serialPort" , serialPort);
        var html =  $('#setting-btn').children("i").prop("outerHTML");
        $('#setting-btn').html(html + serialPort );
        modal.closeModal();
    });
    $('#setting-btn').click(function(e){
        e.preventDefault();
        $("#modal1").openModal();
    });

    (function(){
        var serialPort = KeyValue.get("serialPort");
        var html =  $('#setting-btn').children("i").prop("outerHTML");
        $('#setting-btn').html(html + serialPort );
    })();
    $("#task-setting-btn").click(function(e){
        e.preventDefault();
        $("#task-setting-modal").openModal();

    });

    $("#task-confirm-btn").click(function(e){
        var config = {};
        var modal =  $("#task-setting-modal");
        modal.find("[data-task-id]").each(function(){

            var _this = $(this);
            console.log($(this));

            var use =  _this.find("[data-open-option]").prop("checked");
            var taskId =  _this.attr("data-task-id");


            var option = {};
            console.log(use);
            _this.find("[data-is-option=1]").each(function(){
                var value  = $(this).val();
                var name = $(this).attr("name");
                option[name] = value;
            });
            config[taskId] = {
                enable : use,
                option : option
            }
        });
        KeyValue.set("config" ,JSON.stringify(config));
        modal.closeModal();
    });

    /**
     * 从存储中读取配置，如果读取不到，那么就使用默认值
     */
    (function(){
        var modal =  $("#task-setting-modal");
        var config = KeyValue.get("config");


        if(!config){
            config = {};
        }
        else {
            config = JSON.parse(config);
        }
        var defaultConfig = {};
        modal.find("[data-task-id]").each(function(){

            var _this = $(this);
            var taskId =  _this.attr("data-task-id");
            var option = {};
            _this.find("[data-is-option=1]").each(function(){
                var value  = $(this).attr("default-value")
                var name = $(this).attr("name");
                option[name] = value;
            });
            defaultConfig[taskId] = {
                enable : 1,
                option : option
            }
        });
        var _ = require('underscore');
        _.defaults(config,defaultConfig);

        KeyValue.set("config" ,JSON.stringify(config));

        for(var key in config){
            var p = modal.find("[data-task-id=" + key + "]");
            p.find("[data-open-option]").prop("checked" , config[key].enable);
            var option = config[key].option;
            for(var k in option){
                var op = (p.find('[data-is-option=1][name=' + k + ']'));
                op.val(option[k]);
            }
        }
    })();
});


